package com.itheima.tliasmanagement.service.impl;

import com.itheima.tliasmanagement.mapper.ClazzMapper;
import com.itheima.tliasmanagement.mapper.EmpMapper;
import com.itheima.tliasmanagement.mapper.StudentMapper;
import com.itheima.tliasmanagement.service.ReportService;
import com.itheima.tliasmanagement.vo.EmpJobDataVo;
import com.itheima.tliasmanagement.vo.NameValueVo;
import com.itheima.tliasmanagement.vo.StudentCountDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private EmpMapper empMapper;

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private ClazzMapper clazzMapper;

    @Override
    public EmpJobDataVo empJobData() {
        // 从数据库查
        List<NameValueVo> voList =  empMapper.empJobData();
        if(CollectionUtils.isEmpty(voList)){
            return new EmpJobDataVo();
        }
        // 结果组装
        List<String> jobList = new ArrayList<>();
        List<Integer> dataList = new ArrayList<>();
        for (NameValueVo titleValue : voList) {
            jobList.add(titleValue.getName());
            dataList.add(titleValue.getValue());
        }
        EmpJobDataVo vo = new EmpJobDataVo();
        vo.setDataList(dataList);
        vo.setJobList(jobList);
        return vo;
    }

    public EmpJobDataVo empJobData2() {
        // 从数据库查
        List<NameValueVo> voList =  empMapper.empJobData();
        if(CollectionUtils.isEmpty(voList)){
            return new EmpJobDataVo();
        }
        // 结果组装
        List<String> jobList = voList.stream().map(NameValueVo::getName).collect(Collectors.toList());
        List<Integer> dataList = voList.stream().map(NameValueVo::getValue).collect(Collectors.toList());
        return new EmpJobDataVo(jobList, dataList);
    }

    @Override
    public List<NameValueVo> empGenderData() {
        return empMapper.empGenderData();
    }

    @Override
    public StudentCountDataVo studentCountData() {
        List<NameValueVo> voList = clazzMapper.studentCountData();
        if(CollectionUtils.isEmpty(voList)){
            return new StudentCountDataVo();
        }
        StudentCountDataVo vo = new StudentCountDataVo();
        vo.setClazzList(voList.stream().map(NameValueVo::getName).collect(Collectors.toList()));
        vo.setDataList(voList.stream().map(NameValueVo::getValue).collect(Collectors.toList()));
        return vo;
    }

    @Override
    public List<NameValueVo> studentDegreeData() {
        return studentMapper.studentDegreeData();
    }
}
